package org.bluedream.core.utils.webUtils;

import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class CookieUtil {
    private static final int DEFAULT_MAX_AGE = 30*24*60*60;
    private CookieUtil(){
        throw new AssertionError();
    }

    public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge){
        if (StringUtils.isNotBlank(name)){
            Cookie cookie = new Cookie(name , null);
            cookie.setPath(path);
            cookie.setMaxAge(maxAge);
            try {
                cookie.setValue(URLEncoder.encode(value , "utf-8"));
            }catch (Exception e){
                e.printStackTrace();
            }
            response.addCookie(cookie);
        }
    }

    public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
        setCookie(response, name, value, "/", maxAge);
    }

    public static void setCookie(HttpServletResponse response, String name, String value, String path) {
        setCookie(response, name, value, path, DEFAULT_MAX_AGE);
    }

    public static void setCookie(HttpServletResponse response, String name, String value) {
        setCookie(response, name, value, DEFAULT_MAX_AGE);
    }

    public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name, String path, boolean isRemove){
        String value = null;
        if (StringUtils.isNotBlank(name)){
            Cookie[] cookies = request.getCookies();
            if (cookies != null){
                for (int i = 0; i < cookies.length; i++) {
                    Cookie cookie = cookies[i];
                    if (cookie.getName().equals(name)){
                        try {
                            value = URLDecoder.decode(cookie.getValue() , "utf-8");
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        if (isRemove && response != null){
                            cookie.setPath(path);
                            cookie.setMaxAge(0);
                            response.addCookie(cookie);
                        }
                    }
                }
            }
        }
        return value;
    }

    public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name, boolean isRemove) {
        return getCookie(request, response, name, "/", false);
    }

    public static String getCookie(HttpServletRequest request, String name) {
        return getCookie(request, (HttpServletResponse)null, name, false);
    }

    public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name) {
        return getCookie(request, response, name, false);
    }
}
